﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modbus.Device;

namespace EHR18
{
    public class DataFetcher
    {
        public static readonly DataFetcher instance = new DataFetcher();
        IMaster master = PLCDevice.instance.Master;
        #region 实时统计所需要的数据

        public string IDLong
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 7000, 10),true);
            }
        }
        public string IDShort
        {
            get { 
            return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1,7010,4),true);
            }
        }
        public string yuangong
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 7070, 8), true);
            }
        }
        public string zhiling
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 7080, 8), true);
            }     
        }
        public string wuliao
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 7090, 8), true);
            }
        }
        public ushort StationEnables
        {
            get {
                return master.ReadHoldingRegisters(1, 999, 1)[0];
            }
        }

        public ushort[] HiPotCodes
        {
            get {
                return master.ReadHoldingRegisters(1, 7021, 4);
            }
        }
        public ushort[] LeakageCodes
        {
            get { return master.ReadHoldingRegisters(1, 7031, 2); }
        }
        //图像
        public ushort PhotoCode
        {
            get { return master.ReadHoldingRegisters(1, 7051, 1)[0]; }
        }
        //综测
        public ushort ZCCode
        {
            get { return master.ReadHoldingRegisters(1, 7041, 1)[0]; }
        }

        public ushort KZCode
        {
            get { return master.ReadHoldingRegisters(1, 7061, 1)[0]; }
        }

        public bool isOpenPCMode
        {
            get { return master.ReadCoils(1, 12, 1)[0]; }
        }
        /// <summary>
        /// 获取当前生产的产品的名称
        /// </summary>
        /// <returns></returns>
        public string C_ProductName
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 400, 4));
            }
        }

        /// <summary>
        /// 获取当前生产产品的制令号
        /// </summary>
        /// <returns></returns>
        public string C_ZhilingNo
        {
            get
            {
                return BinaryUtil.BCDUShortsToDouble(master.ReadHoldingRegisters(1, 29, 2)).ToString();
            }
        }

        /// <summary>
        /// 获取当前的订单号
        /// </summary>
        /// <returns></returns>
        public string C_OrderNo
        {
            get
            {
                return BinaryUtil.UshortsToString(master.ReadHoldingRegisters(1, 1900, 12));
            }
        }

        /// <summary>
        /// 获取当前产品计划产量
        /// </summary>
        /// <returns></returns>
        public string C_PlanQty
        {
            get
            {
                return BinaryUtil.BCDUShortsToDouble(master.ReadHoldingRegisters(1, 274, 2)).ToString();
            }
        }

        /// <summary>
        /// 获取已经生产的数量
        /// </summary>
        /// <returns></returns>
        public string C_FinishedQty
        {
            get
            {
                return BinaryUtil.BCDUShortsToDouble(master.ReadHoldingRegisters(1, 232, 2)).ToString();
            }
        }

        /// <summary>
        /// 获取当前的完成率
        ///
        /// </summary>
        /// <returns></returns>
        public string C_FinishRate
        {
            get
            {
                return ((double.Parse(C_FinishedQty) * 1.0) / double.Parse(C_PlanQty)).ToString("P");
            }
        }
        /// <summary>
        /// 小车码
        /// </summary>
        public string C_CarId
        {
            get
            {
                return master.ReadHoldingRegisters(1, 151, 1)[0].ToString();
            }
        }
        /// <summary>
        ///绕线机台号
        /// </summary>
        public string C_RxID
        {
            get
            {
                return master.ReadHoldingRegisters(1, 180, 1)[0].ToString();
            }
        }
        /// <summary>
        ///轴号
        /// </summary>
        public string C_AxisNo
        {
            get
            {
                return master.ReadHoldingRegisters(1, 180, 1)[0].ToString();
            }
        }
        /// <summary>
        /// 绕线结果值
        /// </summary>
        public ushort C_Result
        {
            get
            {
                return master.ReadHoldingRegisters(1, 1158, 1)[0];
            }
        }
        #endregion

        #region 排程页所需要的数据

        #endregion
    }
}
